/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 17.06.13
 * Time: 9:32
 * To change this template use File | Settings | File Templates.
 */
package {
import flash.events.Event;
import flash.geom.Point;

public class ForceVector extends DragableMaterialPoint {
    private var _body:MaterialPoint;
    private var _angle:Number = 0;
    private var _moment:Number = 0;

    public function ForceVector() {
    }

    public function setForceWithVector():void
    {
        // calc moment and angle
        _angle = Math.atan2(position.y- body.position.y, position.x - body.position.x);
        moment = Math.pow(Math.pow(position.x - body.position.x, 2) + Math.pow(position.y- body.position.y, 2), 0.5);
    }

    public function setForceWithPolar():void
    {
        // calc vector (x:y)
        position = new Point(
                body.position.x + moment * Math.cos(_angle),
                body.position.y + moment * Math.sin(_angle));
    }


    override public function dragStart(event:Event):void {
        super.dragStart(event);
        stage.addEventListener(Event.ENTER_FRAME, dragging);
    }

    override public function dragStop(event:Event):void {
        super.dragStop(event);
        stage.removeEventListener(Event.ENTER_FRAME, dragging);
    }

    private function dragging(event:Event):void {
        setForceWithVector();
    }



    public function get body():MaterialPoint {
        return _body;
    }

    public function set body(value:MaterialPoint):void {
        _body = value;
    }

    public function get angle():Number {
        return _angle / Math.PI * 180;
    }

    public function set angle(value:Number):void {
        _angle = value / 180 * Math.PI;
        setForceWithPolar();
    }

    public function get moment():Number {
        return _moment;
    }

    public function set moment(value:Number):void {
        _moment = value;
        setForceWithPolar();
    }
}
}
